package com.mmlc.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/21.
 */
public class FileUtils {

    /**
     * @param fileName   写入文件名
     * @param writeStr   写入字符串
     * @param isContinue 是否续写
     * @return 是否写入成功
     */
    public static boolean writeStringToFile(String fileName, String writeStr, boolean isContinue) {
        if ("".equals(fileName) || "".equals(writeStr)) {
            return false;
        }

        if (!new File(Constants.DIR_CACHE).exists())
            new File(Constants.DIR_CACHE).mkdirs();

        try {
            FileWriter fw = new FileWriter(new File(Constants.DIR_CACHE, fileName + ".txt"), isContinue);
            BufferedWriter bufWriter = new BufferedWriter(fw);
            bufWriter.write(writeStr);
            bufWriter.newLine();
            bufWriter.close();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean writeStringToFile(String fileName, List<String> writeStr) {
        return writeStringToFile(fileName, writeStr, false);
    }

    public static boolean writeStringToFile(String fileName, List<String> writeStr, boolean isContinue) {
        if ("".equals(fileName) || (writeStr == null)) {
            return false;
        }

        if (!new File(Constants.DIR_CACHE).exists())
            new File(Constants.DIR_CACHE).mkdirs();

        try {
            FileWriter fw = new FileWriter(new File(Constants.DIR_CACHE, fileName + ".txt"), isContinue);
            BufferedWriter bufWriter = new BufferedWriter(fw);
            for (int i = 0; i < writeStr.size(); i++) {
                bufWriter.write(writeStr.get(i));
                bufWriter.newLine();
            }
            bufWriter.close();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static ArrayList<String> readStringFromFile(String fileName) {
        return readStringFromFile(fileName, Integer.MAX_VALUE);
    }

    /**
     * @param fileName 读取文件名
     * @return 读取到的字符串
     */
    public static ArrayList<String> readStringFromFile(String fileName, int stringNum) {
        ArrayList<String> strings = new ArrayList<>();
        if (fileName == null || "".equals(fileName)) {
            return strings;
        }

        if (!new File(Constants.DIR_CACHE, fileName + ".txt").exists())
            return strings;

        try {
            FileReader fr = new FileReader(new File(Constants.DIR_CACHE, fileName + ".txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null && !"".equals(line)) {
                strings.add(line);
                if (strings.size() >= stringNum) break;
                line = br.readLine();
            }
            br.close();
            return strings;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strings;
    }

    public static boolean delete(String fileName) {
        return delete(null, fileName);
    }

    public static boolean delete(String dirPath, String fileName) {
        if (fileName == null || "".equals(fileName)) {
            return false;
        }
        dirPath = ViewUtils.isEmpty(dirPath) ? Constants.DIR_CACHE : dirPath;
        if (!new File(dirPath, fileName).exists())
            return true;

        return new File(dirPath, fileName).delete();
    }
}
